<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
  </head>

  <body>

  </body>
  <script>
    /* 
    练习三：判断字符串"dsejscnesjcnssdoisc"中哪个字符出现次数最多并计算次数
    */
    //解法一:
    /* let str = "dsejscnesjcnssdoisc";

    let obj = {};
    for (let i = 0; i < str.length; i++) {
      let char = str.charAt(i);
      if (obj[char]) {
        obj[char]++; //把字符都放进对象里
      } else {
        obj[char] = 1;
      }
    }
    console.log(obj);
    let c = '';
    let count = 0; //记录次数
    for (item in obj) {
      if (obj[item] > count) {
        count = obj[item];
        c = item;
      }
    }
    console.log("dsejscnesjcnssdoisc" + "出现字符次数最多的是" + c + "出现次数为" + count); */


    //2.解法二：
    let str = "dsejscnesjcnssdoisc";
    let arr = [...str];
    let obj = {};
    arr.forEach((item) => {
      if (obj[item]) {
        obj[item]++;
      } else {
        obj[item] = 1
      }
    });
    console.log(obj);
    let count = 0;
    let c = '';

    for (k in obj) {
      if (obj[k] > count) {
        count = obj[k];
        c = k
      }
    }
    console.log("dsejscnesjcnssdoisc" + "出现字符次数最多的是" + c + "出现次数为" + count);
  </script>


</html>